package com.flc.gateway.mq;

import com.flc.gateway.config.AppConfig;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;

/**
 * @author haizi
 * @date 2018/1/23
 */
public class MqProducer {

    private static DefaultMQProducer producer = null;
    private static boolean initialState = false;

    private MqProducer() {}

    public static void init(AppConfig.RocketMqCfg rocketMqCfg) {

        if(producer == null){
            producer = new DefaultMQProducer(rocketMqCfg.getGroupName());
        }

        if(!initialState) {
            producer.setNamesrvAddr(rocketMqCfg.getNameServerAddr());

            try {
                producer.start();
                initialState = true;
            } catch (MQClientException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                initialState = false;
            }
        }
    }

    public static DefaultMQProducer getProducer(){
        return producer;
    }

    public static Boolean getState() {
        return initialState;
    }


}
